/*
 * JBeagle - a Java toolkit for genetic algorithms.
 * 
 * Copyright (c) 2010 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package jbeagle.core.ops;

import jbeagle.core.Individual;
import jbeagle.core.Population;

public abstract class AbstractMutator<I extends Individual<G>, G> implements Mutator<I, G> {
	
	protected double rate = Mutator.DEFAULT_RATE;
	
	protected AbstractMutator() {
		
	}
	
	/**
	 * @param rate The mutation rate.
	 */
	protected AbstractMutator( double rate ) {
		setRate( rate );
	}
	
	public Population<I, G> apply( Population<I, G> pop ) {
		Population<I, G> nextPop = new Population<I, G>( pop.size() );
		for ( I ind : pop ) {
			apply( ind );
			nextPop.add(ind);
		}
		return nextPop;
	}
	
	public abstract void apply( I ind );
	
	/**
	 * Returns the current mutation rate.
	 * 
	 * @return The mutation rate.
	 * @see jbeagle.core.ops.GeneticOperator#getRate()
	 */
	public double getRate() {
		return rate;
	}
	
	/**
	 * Sets the current mutation rate.
	 * 
	 * @param r The desired mutation rate.
	 * @see jbeagle.core.ops.GeneticOperator#setRate( double )
	 */
	public void setRate( double r ) {
		rate = r;
	}
}
